﻿using BaseUI;
using BLL;
using Model;
using System;
using System.Data;
using System.Web.UI.WebControls;

namespace Thksoft.BusinessExpansion.Process
{
    public partial class EditQueue : BasePage
    {
        private Lc_QueueBLL lqb = new Lc_QueueBLL();
        private Lc_ProcessBLL lpb = new Lc_ProcessBLL();
        private Lc_BlockBLL lbb = new Lc_BlockBLL();
        private Lc_NodeBLL lnb = new Lc_NodeBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindProcess();

                BindCompany();

                #region 绑定审核类型

                DataTable QueueTypeData = new DataTable();
                QueueTypeData.Columns.Add("id");
                QueueTypeData.Columns.Add("name");

                QueueTypeData.Rows.Add(new object[] { "0", "单人审核" });
                QueueTypeData.Rows.Add(new object[] { "1", "多人审核一人通过即通过" });
                QueueTypeData.Rows.Add(new object[] { "2", "多人审核全部通过才通过" });
                QueueTypeData.Rows.Add(new object[] { "3", "指定人员办理" });
                QueueTypeData.Rows.Add(new object[] { "4", "默认片区管理员" });
                QueueTypeData.Rows.Add(new object[] { "5", "指定特定操作产生的人员" });
                ddlCheckType.DataSource = QueueTypeData;
                ddlCheckType.DataValueField = "id";
                ddlCheckType.DataTextField = "name";
                ddlCheckType.DataBind();
                ddlCheckType.SelectedValue = "1";

                #endregion 绑定审核类型

                lblCzRen.Text = lognUser.USER_NAME;
                lblCzTime.Text = DateTime.Now.ToString();

                if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
                {
                    //用户修改
                    Bind();
                }
            }
        }

        private void BindCompany()
        {
            DataTable tableOrganization = new Sys_CompanyBLL().GetAllInfo();
            if (tableOrganization != null && tableOrganization.Rows.Count > 0)
            {
                // setList(tableOrganization, ddlCompany);
                PublicMethod.BindDDL(ddlCompany, tableOrganization, "CMY_ID", "CMY_NAME", true);
            }
            else
            {
                ddlCompany.Items.Add(new ListItem("无", "0"));
            }
        }

        private void BindProcess()
        {
            DataTable table = lpb.GetAllInfo("", "");
            if (table != null && table.Rows.Count > 0)
            {
                ddlProcess.DataSource = table;
                ddlProcess.DataTextField = "C_PROCESS_NAME";
                ddlProcess.DataValueField = "N_PROCESS_ID";
                ddlProcess.DataBind();
                ddlProcess.Items.Insert(0, new ListItem("---请选择---", "-1"));
            }
            else
            {
                MessageBox.Show(Page, "请添加流程信息");
                btnSave.Enabled = false;
                return;
            }
        }

        private void Bind()
        {
            //绑定要修改的值
            Lc_Queue model = lqb.GetInfoById(Request.QueryString["ID"].ToString());

            //绑定流程信息
            DataTable table = lpb.GetInfoByIdTab(model.N_PROCESS_ID.ToString(), lognUser.USER_NAME);

            if (table != null && table.Rows.Count > 0)
            {
                ddlProcess.DataSource = table;
                ddlProcess.DataTextField = "C_PROCESS_NAME";
                ddlProcess.DataValueField = "N_PROCESS_ID";
                ddlProcess.DataBind();
                ddlProcess.Items.Insert(0, new ListItem("---请选择---", "-1"));
            }
            else
            {
                MessageBox.Show(Page, "请添加流程信息");
                btnSave.Enabled = false;
                return;
            }
            ddlProcess.SelectedValue = model.N_PROCESS_ID.ToString();

            ddlProcess.Enabled = false;

            //绑定节点信息
            DataTable dtNode = lnb.GetAllInfo("ProcessID", ddlProcess.SelectedValue);

            ddlJiedian.Items.Clear();
            if (dtNode != null && dtNode.Rows.Count > 0)
            {
                ddlJiedian.DataSource = dtNode;
                ddlJiedian.DataTextField = "C_NODE_NAME";
                ddlJiedian.DataValueField = "N_NODE_ID";
                ddlJiedian.DataBind();
                ddlJiedian.Items.Insert(0, new ListItem("---请选择---", "-1"));
            }

            ddlCompany.SelectedValue = model.N_COMPANYID == 0 ? "-1" : model.N_COMPANYID.ToString();

            ddlJiedian.SelectedValue = model.N_NODE_ID.ToString();

            ddlCheckType.SelectedValue = model.N_AUDIT_TYPE.ToString();

            txtZxXh.Value = model.N_QUEUE_NUMBER.ToString();

            ddlblff.SelectedValue = model.N_TRAN_TYPE.ToString();
            ddlblff_SelectedIndexChanged(null, null);

            string result = "";
            //办理人员
            if (!string.IsNullOrEmpty(model.C_QUEUE_TRANSACTION_ID) && model.C_QUEUE_TRANSACTION_ID != "-1")
            {
                result = GetUserNames(model.C_QUEUE_TRANSACTION_ID);
                Names.Value = result;
                HIds.Value = model.C_QUEUE_TRANSACTION_ID;
            }

            if (model.C_POSTS != "" && model.C_POSTS != "-1")
            {
                //角色
                HId.Value = model.C_POSTS.ToString();
                DataTable dt = new Sys_RoleBLL().GetInfoByCondition("RoleIds", HId.Value);
                txtRoleName.Text = "";
                foreach (DataRow dr in dt.Rows)
                {
                    txtRoleName.Text += dr["ROLE_NAME"].ToString() + ",";
                }
            }

            string tblock = "";
            if (!string.IsNullOrEmpty(model.C_QUEUE_BLOCK))
            {
                hidBlockId.Value = model.C_QUEUE_BLOCK == null ? "" : model.C_QUEUE_BLOCK;

                DataTable dtBlock = lbb.GetAllInfo("BlockID", hidBlockId.Value);

                if (dtBlock != null && dtBlock.Rows.Count > 0)
                {
                    foreach (DataRow dr in dtBlock.Rows)
                    {
                        tblock += dr["C_BLOCK_NAME"].ToString() + ",";
                    }
                    tblock = tblock.Substring(0, tblock.Length - 1);
                }
            }
            else
            {
                tblock = "无";
                hidBlockId.Value = "";
            }

            txtBlock.Value = tblock;
            txtGLTB.Text = model.C_SPICAL_TABLE.ToString();
            txtGLRY.Text = model.C_SPICAL_COL.ToString();

            lblCzRen.Text = lognUser.USER_NAME;
            lblCzTime.Text = DateTime.Now.ToString();

            if (model.N_CANCEL == 1)
            {
                chkAllowZX.Checked = true;
            }
            else
            {
                chkAllowZX.Checked = false;
            }

            if (model.N_CHANGE == 1)
            {
                chkSpicaltran.Checked = true;
            }
            else
            {
                chkSpicaltran.Checked = false;
            }
        }

        #region 审核方式发生改变

        protected void ddlType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlCheckType.SelectedValue.Trim() != "5")
            {
                trType.Style.Add("display", "none");
            }
            else
            {
                trType.Style.Add("display", "table-row");//block
            }
        }

        #endregion 审核方式发生改变

        protected void ddlblff_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlblff.SelectedValue.Trim() == "0")
            {
                Rychang.Style["Display"] = "table-row";
                Gwchang.Style["Display"] = "None";
            }
            else
            {
                Rychang.Style["Display"] = "None";
                Gwchang.Style["Display"] = "table-row";
            }
        }

        protected void btnReturn_Click(object sender, EventArgs e)
        {
            MessageBox.ResponseScript(this, "FORM_CLOSE();");
            //window.close();
            // Response.Redirect("/BusinessExpansion/Process/QueueSelect.aspx");
        }

        private void ClearControl()
        {
            ddlProcess.SelectedValue = "-1";
            ddlJiedian.SelectedValue = "-1";
            txtZxXh.Value = "";
            Names.Value = "";
            ddlCheckType.SelectedValue = "-1";
            hidBlockId.Value = "";
            txtBlock.Value = "";
            txtGLTB.Text = "";
            txtGLRY.Text = "";
        }

        protected void ddlProcess_SelectedIndexChanged(object sender, EventArgs e)
        {
            //绑定流程信息
            DataTable dt = lnb.GetAllInfo("ProcessID", ddlProcess.SelectedValue);

            ddlJiedian.Items.Clear();
            if (dt != null && dt.Rows.Count > 0)
            {
                if (dt != null && dt.Rows.Count > 0)
                {
                    ddlJiedian.DataSource = dt;
                    ddlJiedian.DataTextField = "C_NODE_NAME";
                    ddlJiedian.DataValueField = "N_NODE_ID";
                    ddlJiedian.DataBind();
                    ddlJiedian.Items.Insert(0, new ListItem("---请选择---", "-1"));
                    btnSave.Enabled = true;
                }
            }
            else
            {
                MessageBox.Show(Page, "请添加节点信息");
                btnSave.Enabled = false;
                return;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (ddlProcess.SelectedValue.ToString() == "-1")
            {
                MessageBox.Show(Page, "请选择流程");
                return;
            }
            if (ddlJiedian.SelectedValue.ToString() == "-1")
            {
                MessageBox.Show(Page, "请选择节点");
                return;
            }

            if (ddlblff.SelectedIndex == 0)
            {
                //人员办理
                if (HIds.Value == "" || HIds.Value == "-1" && ddlCheckType.SelectedValue.ToString() != "5")
                {
                    MessageBox.Show(Page, "请选择办理人员");
                    return;
                }
            }
            else
            {
                //岗位办理
                if (HId.Value == "-1" || HId.Value == "")
                {
                    MessageBox.Show(Page, "请选择岗位角色");
                    return;
                }
            }
            decimal xh = 0;
            if (!decimal.TryParse(txtZxXh.Value.ToString(), out xh))
            {
                MessageBox.Show(Page, "执行序号请填写数字");
                return;
            }
            Lc_Queue model = new Lc_Queue();

            model.N_PROCESS_ID = decimal.Parse(ddlProcess.SelectedValue.ToString());
            model.N_NODE_ID = decimal.Parse(ddlJiedian.SelectedValue.ToString());
            model.N_QUEUE_NUMBER = decimal.Parse(txtZxXh.Value.ToString());
            model.N_AUDIT_TYPE = decimal.Parse(ddlCheckType.SelectedValue.ToString());
            model.C_QUEUE_BLOCK = hidBlockId.Value.ToString();
            model.C_SPICAL_TABLE = txtGLTB.Text;
            model.C_SPICAL_COL = txtGLRY.Text;
            model.N_TRAN_TYPE = 0;
            model.N_COMPANYID = ddlCompany.SelectedValue == "-1" ? 0 : decimal.Parse(ddlCompany.SelectedValue);

            if (ddlblff.SelectedIndex == 1)
            {
                //岗位办理
                model.N_TRAN_TYPE = 1;
                model.C_POSTS = HId.Value;
                model.C_QUEUE_TRANSACTION_ID = "-1";
            }
            else
            {
                //人员办理
                model.C_POSTS = "-1";
                model.C_QUEUE_TRANSACTION_ID = HIds.Value;
            }
            //model.N_COMPANYID = 0;
            model.N_CANCEL = 0;
            if (chkAllowZX.Checked)
            {
                model.N_CANCEL = 1;
            }

            model.N_CHANGE = 0;
            if (chkSpicaltran.Checked)
            {
                model.N_CHANGE = 1;
            }
            if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
            {
                //用户修改
                //根据队列编号查询队列信息
                model.N_QUEUE_ID = decimal.Parse(Request.QueryString["ID"].ToString());

                int count = new Lc_QueueBLL().UPDATE(model, lognUser.USER_NAME);
                if (count > 0)
                {
                    MessageBox.Show(Page, "保存成功");
                    return;
                }
                else
                {
                    MessageBox.Show(Page, "保存失败");
                    return;
                }
            }
            else
            {
                //用户新增
                int count = new Lc_QueueBLL().INSERT(model, lognUser.USER_NAME);
                if (count > 0)
                {
                    MessageBox.Show(Page, "新增成功");
                }
                else
                {
                    MessageBox.Show(Page, "新增失败");
                }
            }
        }
    }
}